/*****************************************************************************
 *                                                                           *
 *   Copyright (C) 2010 by Luca Cireddu                                      *
 *                         luca@lucacireddu.it                               *
 *                         http://www.lucacireddu.it                         *
 *                                                                           *
 *   This program is free software; you can redistribute it and/or modify    *
 *   it under the terms of the GNU General Public License as published by    *
 *   the Free Software Foundation; either version 2 of the License.          *
 *                                                                           *
 *   This program is distributed in the hope that it will be useful,         *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with this program; if not, write to the                           *
 *   Free Software Foundation, Inc.                                          *
 *   51 Franklin Street, Fifth Floor                                         *
 *   Boston, MA  02110-1301, USA.                                            *
 *                                                                           *
 *****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#include "common.h"
#include "netgraph.h"
#include "defines.h"
#include "version.h"
#include "filesutils.h"
#include "ngexit.h"
#include "stringsutils.h"
#include "parseargs.h"
#include "datacollect.h"
#include "grphimages.h"

graph_param params;
int grph_x;
int grph_y;

int main(int argc, char **argv)
{
    data_collect *alldata, *diffdata;

    signal(SIGINT, sigHandler);

    showWelcome();

    if(params.verbose == B_TRUE)
        printf("Removing old files... ");

    cleanFiles();

    if(params.verbose == B_TRUE)
        printf("done!\n");

    if(parseArgs(argc, argv) == true) {

        do {
            if(params.verbose == B_TRUE)
                printf("Reading values... ");

            readValue();

            if(params.verbose == B_TRUE)
                printf("done!\n");

            if(params.verbose == B_TRUE)
                printf("Collecting data... ");

            alldata = dataCollect();

            if(params.verbose == B_TRUE)
                printf("done!\n");

            if(params.verbose == B_TRUE)
                printf("Parsing data... ");

            diffdata = dataParse(alldata);

            if(params.verbose == B_TRUE)
                printf("done!\n");

            if(params.verbose == B_TRUE)
                printf("Creating image:\n");

            createGraph(diffdata);

            if(params.verbose == B_TRUE)
                printf("Freeing memory... ");

            freeDataCollect(alldata);
            freeDataCollect(diffdata);

            if(params.verbose == B_TRUE)
                printf("done!\n");

            if(params.verbose == B_TRUE)
                printf("Updating image file... ");

            copyFileBinary(IMAGE_TMPFILE, params.outfile);

            if(params.verbose == B_TRUE)
                printf("done!\n");

            if(params.verbose == B_TRUE)
                printf("Sleepping %d seconds: ", params.interval);

            ngSleep(params.interval);

            if(params.verbose == B_TRUE)
                printf("done!\n");
        } while(1);

    }

    return 0;
}

void sigHandler(int sig)
{
    switch(sig) {
        case SIGINT:
            ngExit("SIGINT received");
            break;
    }
}

void ngSleep(int seconds)
{
    int i;

    for(i = 0; i < seconds; i++) {
        if(params.verbose == B_TRUE)
            printf(".");
        sleep(1);
    }
}

void readValue()
{
    FILE *read, *write;
    char **elems;
    char line[1000];
    char c;
    int i, lines;

    lines = 0;
    read = fopen(params.datastorage, "r");

    if(read != NULL) {
        lines = 1;

        while(!feof(read)) {
            c = (char) fgetc(read);
            if(c == '\n')
                lines++;
        }

        fclose(read);

        if(lines > grph_x) {
            read = fopen(params.datastorage, "r");
            write = fopen(STORAGE_TMPFILE, "w");

            for(i = 0; i < (lines - grph_x); i++)
                fgets(line, 999, read);

            while(fgets(line, 150, read) != NULL)
                fprintf(write, "%s\n", line);

            fclose(write);
            fclose(read);

            copyFile(STORAGE_TMPFILE, params.datastorage);
            unlink(STORAGE_TMPFILE);
        }
    }

    read = fopen(KERNEL_TMPFILE, "r");
    write = fopen(params.datastorage, "a");

    while(fgets(line, 150, read) != NULL) {
        elems = explode(cleanString(line), ' ');

        i = 0;

        if(strncmp(elems[0], params.devname, strlen(params.devname)) == 0) {
            fprintf(write, "%ld %s %s\n", (long int) 1, elems[1], elems[9]);
            break;
        }

        explodeFree(elems);

    }

    fclose(read);
    fclose(write);
}

void cleanFiles()
{
    removeFile(params.datastorage);
    removeFile(params.outfile);
    removeFile(IMAGE_TMPFILE);
    removeFile(KERNEL_TMPFILE);
    removeFile(STORAGE_TMPFILE);
}

void showWelcome()
{
    printf("%s started...\n", netgraph_name);
}
